export function ws(port, param, callback) {
    const socket = new WebSocket(`ws://localhost:${port}/listener`)

    // 监听连接打开事件
    socket.onopen = () => {
        console.log('WebSocket连接已打开');
        // 发送消息到服务器
        socket.send(JSON.stringify(param))
    }

    socket.post = msg => {
        socket.send(JSON.stringify(msg))
    }

    // 监听消息事件
    socket.onmessage = event => {
        const response = JSON.parse(event.data)
        // console.log('收到服务器消息: ', response)

        if (response.code) console.log('发生错误！！！')

        callback && callback(response.msg, socket)
    }

    // 监听连接关闭事件
    // socket.onclose = () => {
    //     console.log('WebSocket连接已关闭')
    // }
}